package com.bdqn.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.blog.entity.Singer;
import com.bdqn.blog.service.impl.SingerServiceImpl;
import com.bdqn.blog.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 歌手 前端控制器
 * </p>
 *
 * @author 阿桐幕
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/blog/singer")
public class SingerController {

    @Autowired
    SingerServiceImpl singerService;


    @GetMapping("/findSingerAllOrByName")
    public Object findSingerAllOrByName(String query , Integer pageIndex , Integer pageSize){
        //封装pagehelper的分页对象  参数 页面 页大小
        PageHelper.startPage(pageIndex,pageSize);
        List<Singer> singerList = singerService.findSingerAllOrByName(query);
        //将集合存入到pageinfo对象中
        PageInfo<Singer> pageInfo = new PageInfo<Singer>(singerList);
        return Result.success(pageInfo);
    }

    @GetMapping("/findSingerAllOrBySex")
    public Object findSingerAllOrBySex(String query , Integer pageIndex , Integer pageSize){
        //封装pagehelper的分页对象  参数 页面 页大小
        PageHelper.startPage(pageIndex,pageSize);
        Integer sex = Integer.parseInt(query);
        List<Singer> singerList = singerService.findSingerAllOrBySex(sex);
        //将集合存入到pageinfo对象中
        PageInfo<Singer> pageInfo = new PageInfo<Singer>(singerList);
        return Result.success(pageInfo);
    }

    @PostMapping("/addSinger")
    public Result addSinger(@RequestBody Singer singer){
        boolean save = singerService.save(singer);
        if (save){
            return Result.success();
        }else {
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/getAll")
    public Result getAll(){
        return Result.success(singerService.list());
    }

    /**
     * 更新歌手图片
     */
    @PostMapping("/updateSingerPic")
    @RequestMapping(value = "/updateSingerPic",method = RequestMethod.POST)
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        if(avatorFile.isEmpty()){
            return Result.fail( "文件上传失败");
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径F:\idea-project\music\src\main\resources\static\img\singerPic
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"singerPic";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/singerPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            boolean flag = singerService.saveOrUpdate(singer);
            if(flag){
                return Result.success("上传成功");
            }
            return Result.fail( "文件上传失败");
        } catch (IOException e) {

            return Result.fail( "文件上传失败")+e.getMessage();
        }
    }
    @PostMapping("/getSingerByid/{id}")
    public Result getSingerByid(@PathVariable Integer id){
        return Result.success(singerService.getById(id));
    }

    @PostMapping("/delSingerByd/{id}")
    public Result delSingerByd(@PathVariable Integer id){
        return Result.success(singerService.removeById(id));
    }

    @PostMapping("/SaveSinger")
    public Result SaveSinger(@RequestBody Singer singer){
        System.out.println(singer.getId());
        boolean save = singerService.saveOrUpdate(singer);
        if (save){
            return Result.success();
        }else {
            return Result.fail("修改失败");
        }
    }

}

